using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Web;
using System.IO;

using OrbitOne.Feed.Domain;
using OrbitOne.Feed.Interfaces;

namespace OrbitOne.Feed.Handlers {
    /// <summary>
    /// Writes podcast formatted xml in the http ouputstream
    /// </summary>
    internal class PodCastWriter : RSSWriter {
        #region Ctor's :
        public PodCastWriter(PodCastWebFeed webFeed)
            : base(webFeed) {
        }
        #endregion Ctor's
        #region Properties :
        protected PodCastWebFeed podcastWebFeed {
            get { return base.webFeed as PodCastWebFeed;  }
        }
        #endregion Properties
        #region Methods :
        /// <summary>
        /// Writes the actual feed in to the Http output stream.
        /// This method must be implemented by specific Writers like the Atom, RSS or podcast writers.
        /// </summary>
        /// <param name="webFeed">The web feed.</param>
        public override void WriteFeed() {
            base.WriteFeed();
        }
        /// <summary>
        /// Writes the feed info.
        /// </summary>
        /// <param name="webFeed">The web feed.</param>
        protected override void BeginWriteFeedInfo() {
            base.BeginWriteFeedInfo();

            base.WriteElementValue("itunes:subtitle", podcastWebFeed.SubTitle);

            if (podcastWebFeed.HasITunesAuthor) {
                base.WriteElementValue("itunes:author", podcastWebFeed.ITunesAuthor.Name);
            }

            base.WriteElementValue("itunes:summary", podcastWebFeed.Summary);
            if (podcastWebFeed.Owner != null) {
                _writer.WriteStartElement("itunes:owner");
                base.WriteElementValue("itunes:name", podcastWebFeed.Owner.Name);
                base.WriteElementValue("itunes:email", podcastWebFeed.Owner.Email);
                _writer.WriteEndElement();
            }

            _writer.WriteStartElement("itunes:category");
            base.WriteAttributeValue("text", podcastWebFeed.Category);
            if (podcastWebFeed.HasSubCategory) {
                _writer.WriteStartElement("itunes:category");
                base.WriteAttributeValue("text", podcastWebFeed.SubCategory);
                _writer.WriteEndElement();
            }
            _writer.WriteEndElement();

            if (podcastWebFeed.Image != null) {
                _writer.WriteStartElement("itunes:image");
                WriteAttributeValue("href", podcastWebFeed.Image.ToString());
                _writer.WriteEndElement();
            }

            if (podcastWebFeed.HasKeywords) {
                base.WriteElementValue("itunes:keywords", String.Join(",", podcastWebFeed.KeyWords.ToArray()));
            }

            base.WriteElementValue("itunes:explicit", podcastWebFeed.Explicity.ToString());
            if (podcastWebFeed.HasNewFeedUrl) {
                base.WriteElementValue("itunes:new-feed-url", podcastWebFeed.NewFeedUrl.ToString());
            }
        }
        /// <summary>
        /// Writes the root attributes.
        /// </summary>
        protected override void WriteRootAttributes() {
            WriteAttributeValue("xmlns:itunes", "http://www.itunes.com/dtds/podcast-1.0.dtd");
            base.WriteRootAttributes();
        }
        /// <summary>
        /// Write topic extensions
        /// </summary>
        protected override void WriteTopicExtentions(Topic topic) {
            if (topic is PodCastTopic) {
                PodCastTopic podCastTopic = topic as PodCastTopic;

                if (topic.HasAuthor) {
                    base.WriteElementValue("itunes:author", topic.Author.Name);
                }
                base.WriteElementValue("itunes:subtitle", podCastTopic.SubTitle);
                base.WriteElementValue("itunes:summary", podCastTopic.Summary);
                base.WriteElementValue("itunes:duration", podCastTopic.Duration.ToString());
                base.WriteElementValue("itunes:explicit", podCastTopic.Explicity.ToString());
                if (podCastTopic.HasKeyWords) {
                    base.WriteElementValue("itunes:keywords", String.Join(",", podCastTopic.KeyWords.ToArray()));
                }
            }
        }
        #endregion Methods
    }
}
